<template>
  <j-modal
    :title="title"
    :width="1200"
    :visible="visible"
    :maskClosable="false"
    :confirmLoading="confirmLoading"
    switchFullscreen
    @ok="handleOk"
    @cancel="handleCancel">
    <a-spin :spinning="confirmLoading">
      <!-- 主表单区域 -->
      <a-form :form="form">
        <a-row>

	
        
	
         <a-col :span="12">
            <a-form-item label="类型名称" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['description']" placeholder="请输入类型名称"></a-input>
            </a-form-item>
         </a-col>

         <a-col :span="12">
            <a-form-item label="父类型" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-dict-select-tag type="list" v-decorator="['parentId']" :trigger-change="true" dictCode="DictionaryType,description,dictionaryTypeId," placeholder="请选择父类型"/>
            </a-form-item>
         </a-col>
        </a-row>
      </a-form>

      <!-- 子表单区域 -->
      <a-tabs v-model="activeKey" @change="handleChangeTabs">
        <a-tab-pane tab="数据字典明细" :key="refKeys[0]" :forceRender="true">
          <j-editable-table
            :ref="refKeys[0]"
            :tableName="refKeys[0]"
            :loading="dataDictionaryTable.loading"
            :columns="dataDictionaryTable.columns"
            :dataSource="dataDictionaryTable.dataSource"
            :maxHeight="300"
            :rowNumber="true"
            :rowSelection="true"
            :actionButton="true"/>
        </a-tab-pane>
        
      </a-tabs>

    </a-spin>
  </j-modal>
</template>

<script>

  import pick from 'lodash.pick'
  import { FormTypes,getRefPromise } from '@/utils/JEditableTableUtil'
  import { JEditableTableMixin } from '@/mixins/JEditableTableMixin'
  import { validateDuplicateValue } from '@/utils/util'
  import JDictSelectTag from "@/components/dict/JDictSelectTag"
  import { httpAction, getAction } from '@/api/manage'
  export default {
    name: 'DictionaryTypeModal',
    mixins: [JEditableTableMixin],
    components: {
      JDictSelectTag,
    },
    data() {
      return {
        labelCol: {
          xs: { span: 24 },
          sm: { span: 6 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        labelCol2: {
          xs: { span: 24 },
          sm: { span: 3 },
        },
        wrapperCol2: {
          xs: { span: 24 },
          sm: { span: 20 },
        },
        // 新增时子表默认添加几行空数据
        addDefaultRowNum: 1,
        validatorRules: {
        },
        refKeys: ['dataDictionary', ],
        tableKeys:['dataDictionary', ],
        activeKey: 'dataDictionary',
        // 数据字典明细
        dataDictionaryTable: {
          loading: false,
          dataSource: [],
          columns: [
            {
              title: '字典名称',
              key: 'description',
              type: FormTypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue: '',
            },
//            {
//              title: '父ID',
//              key: 'parentId',
//              type: FormTypes.select,
//              dictCode:"DataDictionary,description,dictionaryId",
//              width:"200px",
//              placeholder: '请输入${title}',
//              defaultValue: '',
//            },
            {
              title: '字典编码',
              key: 'dictionaryCode',
              type: FormTypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue: '',
            },
            {
              title: '序列标识',
              key: 'sequenceId',
              type: FormTypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue: '',
            },
          ]
        },
        url: {
          add: "/online/special/api/createDictionaryType",
          edit: "/online/special/api/updateDictionaryType",
          dataDictionary: {
            list: "/online/api/viewDataDictionaryByDictionaryTypeId",
            deleteBatch: "/online/api/deleteDataDictionaryByDictionaryTypeBatch",
          },
        }
      }
    },
    methods: {
      request(formData) {
        let url = this.url.add, method = 'post'
        if (this.model.dictionaryTypeId) {
          url = this.url.edit
          method = 'put'
        }
        this.confirmLoading = true
        httpAction(url, formData, method).then((res) => {
          if (res.success) {
            this.$message.success("成功")
            this.$emit('ok')
            this.close()
          } else {
            this.$message.warning(res.message)
          }
        }).finally(() => {
          this.confirmLoading = false
        })
      },
      getAllTable() {
        let values = this.tableKeys.map(key => getRefPromise(this, key))
        return Promise.all(values)
      },
      /** 调用完edit()方法之后会自动调用此方法 */
      editAfter() {
        let fieldval = pick(this.model,'dictionaryTypeId','parentId','description')
        this.$nextTick(() => {
          this.form.setFieldsValue(fieldval)
        })
        // 加载子表数据
        if (this.model.dictionaryTypeId) {
          let params = { dictionaryTypeId: this.model.dictionaryTypeId }
          this.requestSubTableData(this.url.dataDictionary.list, params, this.dataDictionaryTable)
        }
      },
      /** 整理成formData */
      classifyIntoFormData(allValues) {
        let main = Object.assign(this.model, allValues.formValue)

        return {
          ...main, // 展开
          DataDictionary: allValues.tablesValue[0].values,
        }
      },
      validateError(msg){
        if(msg){
          this.isHandleOk = false;
        }
        this.$message.error(msg)
      },
     popupCallback(row){
       this.form.setFieldsValue(pick(row,'dictionaryTypeId','parentId','description'))
     },

    }
  }
</script>

<style scoped>
</style>
